package com.dalor.phonesmanager.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.dalor.phonesmanager.R;
import com.dalor.phonesmanager.bean.Image;

import java.io.File;
import java.util.ArrayList;

/**
 * 作者： 吴昶 .
 * 时间: 2018/12/28 15:52
 * 功能简介：用两个 imageView 叠加展示所有的图片，暂时无法添加长按事件
 * 作废
 */
public class ShowClickImageActivity extends AppCompatActivity {

    private ImageView imageView;
    private ImageView bgImageView;
    private String imagePath;
    private ArrayList<Image> phones;
    private int index;
    private int lastBg;
    private LinearLayout linearLayout;

    private float touchX;
    private float touchY;
    private float locatX;
    private float locatY;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_big_img);
        imageView=(ImageView)findViewById(R.id.iv_big_img);
        bgImageView=(ImageView)findViewById(R.id.iv_bg_n_l);
        linearLayout=(LinearLayout)findViewById(R.id.linear_big_img);
        imagePath=getIntent().getStringExtra("imagePath");
        phones=getIntent().getParcelableArrayListExtra("phones");
        index=getIntent().getIntExtra("index",0);
        Glide.with(this).load(new File(imagePath)).into(imageView);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("*********"," right: "+imageView.getRight()+"");
                touchX=event.getX();
                touchY=event.getY();
                locatX=touchX;
                locatY=touchY;
                break;
            case MotionEvent.ACTION_MOVE:
                float x=event.getX();
                if(x>touchX){
                    setBgImage(-1);
                }else {
                    setBgImage(1);
                }
                if(Math.sqrt((x-locatX)*(x-locatX)+(event.getY()-locatY)*(event.getY()-locatY))>100)
                tran(locatX,
                        0,
                        x,
                        0,0);
                break;
            case MotionEvent.ACTION_UP:
                float xe=event.getX();
                Log.d("*********"," distance : "+Math.abs((xe-touchX))+"");
                if(xe-touchX==0) {

                }else if(Math.abs((xe-touchX))>300){
                    if(xe>touchX){
                        lastBg=-1;
                        tran(xe,0,linearLayout.getRight()+touchX,0,400);
                    }else {
                        lastBg=1;
                        tran(xe,0,-linearLayout.getRight()+touchX,0,400);
                    }
                }else {
                    lastBg=0;
                    tran(xe,0,0+touchX,0,400);
                }
                break;
        }
        return true;
    }

    private void setBgImage(int t){
        int d=index+t;
        if(d<0 || d>=phones.size()){
            bgImageView.setImageBitmap(null);
            resetImage(t);
            return;
        }
        if(lastBg==d) return;
        Glide.with(this).load(new File(phones.get(d).path)).into(bgImageView);
        lastBg=d;
    }

    private void changeImage(int t){
        if(t==0) return;
        int d=index+t;
        if(d<0 || d>=phones.size()){
            resetImage(t);
            return;
        }
        Glide.with(this).load(new File(phones.get(d).path)).into(imageView);
        resetImage(t);
        index=d;
    }

    private void resetImage(int t){
        if(t==-1){
            tran(linearLayout.getRight()+touchX,0,0+touchX,0,0);
        }else {
            tran(-linearLayout.getRight()+touchX,0,0+touchX,0,0);
        }
    }

    private void tran(float xs, float ys, float xe, float ye, final long duration){
        TranslateAnimation animation=new TranslateAnimation(xs-touchX,xe-touchX,0,0);
        animation.setFillAfter(true);
        animation.setDuration(duration);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if(duration!=0){
                    changeImage(lastBg);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        linearLayout.startAnimation(animation);
        locatX=xe;
        locatY=ye;
    }

}
